/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bluenode.paper;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import java.util.Arrays;

public class PaperView extends View {
	private int w, h;
	private Paint textPaint;
	private MotionEvent touch;
	
    public PaperView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
    }

    private Paint getTextPaint(){
    	if (textPaint == null) {
    		textPaint = new Paint();
    		//textPaint.setColor(Color.BLACK);
    		textPaint.setTextSize(16.0f);
    		//textPaint.setTypeface(Typeface.SANS_SERIF);
    		textPaint.setAlpha(128);
    	}
    	return textPaint;
    }
    @Override
    protected void onDraw(Canvas canvas) {
    	Paint p = getTextPaint();
    	canvas.drawColor(Color.WHITE);
    	float x = 0.0f, y = 0.0f, dy = p.getFontSpacing();
    	canvas.drawText("w=" + w + " h=" + h, x, y += dy, p);
    	canvas.drawText(String.format("density=%s", canvas.getDensity()), x, y += dy, p);
    	if (touch != null) {
        	//canvas.drawText(String.format("%s",touch), x, y += dy, p);
        	for (int i = 0; i < touch.getPointerCount(); i++) {
        		float px = touch.getX(i);
        		float py = touch.getY(i);
            	canvas.drawText(String.format("%d %f.1 %f.1", touch.getAction(), px, py), x, y += dy, p);
            	if (touch.getActionMasked() != MotionEvent.ACTION_POINTER_UP) {
            	  canvas.drawCircle(px, py, 100, p);
            	}
        	}
    	}
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w = w;
        this.h = h;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	touch = event;
    	invalidate();
    	return true;
    }
}
